<?php

/**
 * Created by JetBrains PhpStorm.
 * User: Grom Hellscream
 * Date: 11/2/12
 * Time: 1:19 PM
 * To change this template use File | Settings | File Templates.
 */
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// Include dependancy of the main model form
jimport('joomla.application.component.modelform');
// import Joomla modelitem library
jimport('joomla.application.component.modelitem');
// Include dependancy of the dispatcher
jimport('joomla.event.dispatcher');

/**
 * Themdonvivaokhoi Model
 */
class VastModelThemdonvivaokhoi extends JModelForm {

    public function createItem($data) {
        // set the data into a query to update the record
        $db = $this->getDbo();
        
        $query = 'INSERT INTO #__donvi(' . 
            $db->quote('ten') . ',' . 
            $db->quote('diachi') . ',' .
            $db->quote('sodienthoai') . ',' .
            $db->quote('fax') . ',' .
            $db->quote('thudientu') . ',' .
            $db->quote('hinhanh') . ',' . 
            $db->quote('website') . ',' .
            $db->quote('hoidongkhoahoc') . ',' . 
            $db->quote('chucnang') . ',' . 
            $db->quote('nhiemvu') . ',' .
            $db->quote('hoatdongthuongxuyen') . ',' .
            $db->quote('thongtinkhac') . ',' .
            $db->quote('khoi_id') . ')';
        $query .= ' VALUES(';
        $query .= $db->quote($data['ten']) . ',' .
                  $db->quote($data['diachi']) . ',' .
                  $db->quote($data['sodienthoai']) . ',' .
                  $db->quote($data['fax']) . ',' .
                  $db->quote($data['thudientu']) . ',' .
                  $db->quote($data['hinhanh']) . ',' .
                  $db->quote($data['website']) . ',' .
                  $db->quote($data['hoidongkhoahoc']) . ',' .
                  $db->quote($data['chucnang']) . ',' . 
                  $db->quote($data['nhiemvu']) . ',' .
                  $db->quote($data['hoatdongthuongxuyen']) . ',' .
                  $db->quote($data['thongtinkhac']) . ',' . 
                  $data['khoi_id'] . ')';                         
        
        $db->setQuery($query);
                
        if (!$db->query()) {
            JError::raiseError(500, $db->getErrorMsg());
            return false;
        } else {
            return true;
        }
    }

    public function getForm($data = array(), $loadData = true) {
        // Get the form.
        $form = $this->loadForm('com_vast.themdonvivaokhoi', 'themdonvivaokhoi', array('control' => 'jform', 'load_data' => true));
        if (empty($form)) {
            return false;
        }
        $jinput = JFactory::getApplication()->input;

        // The front end calls this model and uses a_id to avoid id clashes so we need to check for that first.
        if ($jinput->get('a_id')) {
            $id = $jinput->get('a_id', 0);
        }
        // The back end uses id so we use that the rest of the time and set it to 0 by default.
        else {
            $id = $jinput->get('id', 0);
        }

        return $form;
    }

    protected function loadFormData()
    {
        $data = array();

        $data['khoi_id'] = JRequest::getInt('id', 0);
                        
        return $data;
    }
    
    public function getKhoi()
    {
        $id = JRequest::getInt('id');
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        
        $query->select('a.*');
        $query->from('#__donvi as a');
        $query->where('a.id = ' . $id);
        
        $db->setQuery($query);
        $donvi = $db->loadObject();
        
        if ($donvi !== NULL)
        {
            $khoiId = $donvi->khoi_id;
            $query = $db->getQuery(true);
            
            $query->select('a.*');
            $query->from('#__khoi as a');
            $query->where('a.id = ' . $khoiId);
            
            $db->setQuery($query);

            return $db->loadObject();                
        }
        
        return NULL;    
    }      
}